<?php
// +----------------------------------------------------------------------
// | Author: Stupboy King
// +----------------------------------------------------------------------
namespace app\api\model;
use app\api\model\Common;
use \think\Config;
use think\Db;
class OrderFee extends Common
{
    /**
     * 订单费用计算
     * @param $oid 订单id
     */
    public function countFee($oid){
        $fee1 = 'start_fee + wait_fee + time_fee + travel_fee + remote_fee';
        $fee1.= ' + low_fee + night_fee + high_fee + toll + park_fee + other_fee + cancel_fee';
        $fee2 = 'start_fee2 + wait_fee2 + time_fee2 + travel_fee2 + remote_fee2';
        $fee2.= ' + low_fee2 + night_fee2 + high_fee + toll + park_fee + other_fee + cancel_fee';
        $data['driver_money']   = ['exp',$fee2];
        $data['passenger_money']= ['exp',$fee1];
        $isLaw = Db::name('order')->where('order_id',$oid)->value('use_type');
        // 行政执法订单乘客不收费用
        $isLaw == 1 && $data['passenger_money'] = 0;
        $data['company_money']  = ['exp',$fee1.' - ('.$fee2.')'];
        $this->save($data,['order_id'=>$oid]);
    }

    /**
     * 获取司机费用
     * @param $oid
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function getDriverFee($oid){
        $this->countFee($oid);
        $field = 'start_fee2 start_fee,travel_fee2 travel_fee,meter,use_time,time_fee2 time_fee,low_speed_time low_time';
        $field.= ',low_fee2 low_fee,remote_meter,remote_fee2 remote_fee,wait_time,wait_fee2 wait_fee,driver_money';
        $field.= ',toll,park_fee,other_fee,high_fee,other_note,cancel_fee';
        $data  = $this->where('order_id',$oid)->field($field)->find();
        $data['use_time'] = round($data['use_time']/60,0);
        $data['wait_time']= round($data['wait_time']/60,0);
        $data['meter']    = round($data['meter']/1000,2);
        $data['low_time'] = round($data['low_time'],0);
        $data['remote_meter'] = round($data['remote_meter']/1000,2);
        return $data;
    }

    /**
     * 获取乘客费用
     * @param $oid
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function getPassengerFee($oid){
        $this->countFee($oid);
        $field = 'start_fee,travel_fee,meter,use_time, time_fee,low_speed_time low_time';
        $field.= ', low_fee,remote_meter, remote_fee,wait_time, wait_fee,passenger_money';
        $field.= ',toll,park_fee,other_fee,high_fee,other_note,cancel_fee';
        $data  = $this->where('order_id',$oid)->field($field)->find();
        $data['use_time'] = round($data['use_time']/60,0);
        $data['wait_time']= round($data['wait_time']/60,0);
        $data['meter']    = round($data['meter']/1000,2);
        $data['low_time'] = round($data['low_time'],0);
        $data['remote_meter'] = round($data['remote_meter']/1000,2);
        return $data;
    }

    /**
     * 更新订单费用
     * @param $oid
     * @param $data
     */
    public function updateFee($oid,$data){
        $fee = $this->where('order_id',$oid)->field('order_id')->find();
        if(empty($fee)){
            $data['order_id'] = $oid;
            $this->save($data);
        }else{
            $this->save($data,['order_id'=>$oid]);
        }
    }

    /**
     * 预计费用计算
     * @param array $car_type 车型信息
     * @param int $mile 里程
     * @param int $time 时间
     * @return mixed
     */
    public function countPreFee($carType=[],$meter=0,$time=0,$isReturn=0){
        // TODO夜间费用
        $starting_price  = $carType['starting_price'];
        $starting_km     = $carType['starting_km'];
        $mileage_cost    = $carType['mileage_cost'];
        $transit_cost    = $carType['transit_cost'];
        $time_cost       = $carType['time_charge'];
        $mileFee         = $meter/1000 - $starting_km > 0 ? ($meter/1000-$starting_km)*$mileage_cost : 0;
        $remoteMile      = $meter/1000 - Config::get('base.remote_km');
        $timeFee         = $time_cost * $time /60;
        $remoteMile      = $remoteMile > 0 ? $remoteMile : 0;
        $remoteFee       = $remoteMile > 0 && $transit_cost >0 ? $remoteMile*$transit_cost :0;
        $fee             = $starting_price + $mileFee + $remoteFee + $timeFee;
        $data['starting_price'] = $starting_price;  //起步价
        $isReturn        = $isReturn == 1 ? 1 : 9;

        Config::set(include  APP_PATH.'/../config/api/config.php','api');
        $data['estimate_cost']  = $isReturn==9 ? $fee :  $fee*(1+Config::get('api.order_return_discount')/100);  //预计费用
        return $data;
    }
}